package com.mind.customer.utils;

import com.mind.api.utils.BusinessCateUtils;
import com.mind.cache.service.CacheService;
import com.mind.common.utils.StringUtils;
import com.mind.customer.mapper.AjRuleMapper;
import com.mind.customer.mapper.AjTicketTypeMapper;
import com.mind.system.domain.AjReviewConfig;
import com.mind.system.domain.AjRule;
import com.mind.system.domain.AjTicketTask;
import com.mind.system.domain.AjTicketType;
import com.mind.system.domain.vo.CateResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class CateUtils {


    @Resource
    private AjRuleMapper ajRuleMapper;

    @Resource
    private CacheService cacheService;
    private Map<String, String> ticketTypeMapping = new HashMap<>();

    @Resource
    private AjTicketTypeMapper ajTicketTypeMapper;

    @Value("${shareCenter.specialBusinessCateNames:}")
    private String specialBusinessCateName;
    @Resource
    private AjTicketTypeMapper ticketTypeMapper;


    /**
     * 存在小类
     * @param businessCate
     * @return
     */
    public CateResult existsCate(String ruleName, String businessCate, String ticketType) {
        CateResult cateResult = new CateResult(){{setExists(false);}};
        if(StringUtils.isEmpty(ruleName)){
            cateResult.setExists(false);
            return cateResult;
        }
        List<AjRule> rules = ajRuleMapper.selectRuleByParams(new AjRule() {
            {
                setTicketType(ticketType);
                setRuleDetailName(ruleName);
            }
        });
        if(rules.size()==0){
            cateResult.setExists(false);
            return cateResult;
        }
        String offLineCateStr = "";
        for(AjRule rule : rules){
            if(StringUtils.isEmpty(rule.getBusinessCate()))
                continue;
            offLineCateStr += rule.getBusinessCate() + ",";

        }
        Set<String> offLineCateSet = Arrays.stream(offLineCateStr.split(",")).collect(Collectors.toSet());
        if(ruleName.contains("混报业务") ){
            if(StringUtils.isNotEmpty(specialBusinessCateName) && StringUtils.isNotEmpty(businessCate) && businessCate.split(",").length > 1 &&BusinessCateUtils.containOffline(Arrays.stream(specialBusinessCateName.split(",")).collect(Collectors.toSet()),  getSmallCate(businessCate))) {
                cateResult.setExists(true);
                cateResult.setRules(rules);
                return cateResult;
            }
        }else {
            cateResult.setExists(BusinessCateUtils.containOffline(offLineCateSet, businessCate));
            cateResult.setRules(rules);
        }
        return cateResult;
    }
    public String getTicketTypeName(String ticketType) {
        if (StringUtils.isEmpty(ticketType)) {
            return ticketType;
        }
        List<String> ticketTypes = new ArrayList<>();
        for (String ticketTypeName : ticketType.split(",")) {
            if (!ticketTypeMapping.containsKey(ticketTypeName)) {
                List<AjTicketType> ajTicketTypes = ticketTypeMapper.selectTicketTypeList(new AjTicketType() {{
                    setCode(ticketTypeName);
                }});
                if (ajTicketTypes.size()>0) {
                    ticketTypeMapping.put(ticketTypeName, ajTicketTypes.get(0).getName());
                }
            }
            ticketTypes.add(ticketTypeMapping.get(ticketTypeName));
        }
        return StringUtils.join(ticketTypes, ",");
    }
    public String getMainCate(String businessMainCate) {
        Map<String, String> mainCate = null;
        if (cacheService.hasKey("mainCateCache")) {
            mainCate = cacheService.getCacheObject("mainCateCache");
        }
        if(mainCate == null) {
            return businessMainCate;
        }
        if(StringUtils.isEmpty(businessMainCate)){
            return "";
        }
        return mainCate.getOrDefault(businessMainCate, businessMainCate);

    }

    public String getSmallCate(String businessCate) {
        Map<String, String> subCate = null;
        if (cacheService.hasKey("subCateCache")) {
            subCate = cacheService.getCacheObject("subCateCache");
        }
        if(subCate == null) {
            return businessCate;
        }
        if(StringUtils.isEmpty(businessCate)){
            return "";
        }
        Map<String, String> finalSubCate = subCate;
        return Arrays.stream(businessCate.split(",")).map(item -> finalSubCate.getOrDefault(item, item)).collect(Collectors.joining(","));
    }

    public String getTicketTypeByCode(String ticketCode){
        List<AjTicketType> ajTicketTypes = ajTicketTypeMapper.selectTicketTypeList(new AjTicketType() {{
            setCode(ticketCode);
        }});
        if(ajTicketTypes.size()==0){
            return ticketCode;
        }
        return ajTicketTypes.get(0).getName();
    }

    public boolean isMatched(AjReviewConfig reviewConfig, AjTicketTask task, boolean containEmpty) {
        Set<String> ticketTypeSet = new HashSet<>();
        if(StringUtils.isNotEmpty(reviewConfig.getTicketTypeName())) {
            ticketTypeSet = Arrays.stream(reviewConfig.getTicketTypeName().split(",")).collect(Collectors.toSet());
        }
        Set<String> offLineCateSet = new HashSet<>();
        if(StringUtils.isNotEmpty(reviewConfig.getBusinessCate())){
            offLineCateSet = Arrays.stream(reviewConfig.getBusinessCate().split(",")).collect(Collectors.toSet());
        }
        String ticketTypeName = getTicketTypeByCode(task.getTicketType());
        return (containEmpty && StringUtils.isEmpty(reviewConfig.getBusinessCate()) && StringUtils.isEmpty(reviewConfig.getTicketTypeName()))
                || ticketTypeSet.contains(ticketTypeName) || offLineCateSet.contains(task.getBusinessCate()) ||
                (StringUtils.isNotEmpty(task.getBusinessCate()) && StringUtils.isNotEmpty(task.getTicketType()) && offLineCateSet.contains(task.getBusinessCate()) && ticketTypeSet.contains(ticketTypeName));
    }

}
